﻿package Cain.MkMc 
{
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import com.greensock.TweenLite;
	/**
	 * 對話框 v1.01 2010.10.15
	 * @author Cain
	 */
	public class CoBk extends Sprite{
		private var misson:String; //訊息
		/**
		 * @private
		 */
		protected const D:int = 20; //外框距離
		/**
		 * @private
		 */
		protected const R:int = 8; //圓角
		/**
		 * @private
		 */
		protected var sp_y:Number;
		/**
		 * @private
		 */
		protected var textFild:TextField;
		/**
		 * @private
		 */
		protected var sp:Sprite;
		public function CoBk(_str:String) 
		{
			misson = _str;
			setup();
		}
		
		private function setup():void
		{
			blendMode = BlendMode.LAYER;
			alpha = 1;
			buttonMode = true;
			wd_setup();//設定文字
			sp_setup();//設定對話框
			addEventListener(Event.ADDED_TO_STAGE, addthis);
		}
		/**
		 * 建構元件與底圖
		 * @private
		 */
		protected function sp_setup():void
		{
			sp = new Sprite();
			var tf_ww:Number = textFild.width;
			var tf_hh:Number = textFild.height;
			sp.graphics.beginFill(0xFFFFFF);
			sp.graphics.drawRoundRect(0, 0, tf_ww + D * 2, tf_hh + D * 2, R, R);
			sp.graphics.endFill();
			textFild.x = D;
			textFild.y = D;
			sp.addChild(textFild);
			sp.x = -sp.width>>1;
			sp.y = -sp.height>>1;
			sp_y = sp.y;
			addChild(sp);
		}
		
		private function wd_setup():void
		{
			textFild = new TextField();
			textFild.autoSize = TextFieldAutoSize.LEFT;
			textFild.textColor = 0;
			textFild.selectable = false;
			textFild.multiline = true;
			textFild.text = misson;
			textFild.mouseEnabled = false;
		}
		
		private function addthis(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addthis);
			addEventListener(Event.REMOVED_FROM_STAGE, renovethis);
			init();
		}
		
		private function init():void
		{
			sp.alpha = 0;
			TweenLite.to(sp, .5, {alpha:1} );
		}
		public function unload():void 
		{
			TweenLite.to(sp, .5, {alpha:0,onComplete:end} );
		}
		
		private function end():void
		{
			parent.removeChild(this);
		}
		private function renovethis(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, renovethis);
		}
	}
	
}